package es.mahulo.battleship.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.mahulo.battleship.api.dao.GameDao;
import es.mahulo.battleship.model.Game;
import es.mahulo.battleship.model.User;

@Repository
public class GameDaoImpl implements GameDao{
	
	private EntityManager entityManager;
	@PersistenceContext  
    public void setEntityManager(EntityManager entityManager)   {  
		this.entityManager = entityManager;  
    }
	
	@Override
	@Transactional
	public Game find(Long id) {
		return entityManager.find(Game.class, id);
	}


	@Override
	@Transactional
	public Game create(final Game game){
		entityManager.persist(game);
		return game;
	}

	@Override
    public Game update(final Game game) {
        return entityManager.merge(game);    
    }

	@Override
	public List<Game> getAllFromUser(User user) {
		Query q = entityManager.createQuery("SELECT g FROM Game g Where g.",Game.class);

		return q.getResultList();
	}
}
